package com.zuoshenge.main.controller;

import com.zuoshenge.main.dto.CourseSelectionDTO;
import com.zuoshenge.main.entity.Course;
import com.zuoshenge.main.service.CourseService;
import com.zuoshenge.redis.util.RedisUtil;
import com.zuoshenge.util.CaffeineCacheUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    private AmqpTemplate rabbitTemplate;
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private CourseService courseService;
    @Resource
    private CaffeineCacheUtil caffeineCacheUtil;
    @Value("${redis.keys.course-quota-key}")
    private String COURSE_QUOTA_KEY;
    @Value("${init.cache.course-cache-key}")
    private String COURSE_CACHE_KEY;

    @PostMapping("selection")
    public ResponseEntity<Object> selection(@RequestBody CourseSelectionDTO courseSelectionDTO) {
        Map<String, Object> result = new HashMap<>();
        Integer currentQuota = (Integer)redisUtil.get(COURSE_QUOTA_KEY + courseSelectionDTO.getCid());
        Course course = (Course) caffeineCacheUtil.getIfPresent(COURSE_CACHE_KEY+courseSelectionDTO.getCid());
        // 如果选课人数已满就不让选课
        if (currentQuota >= course.getQuota()) {
            result.put("msg", "当前课程选课人数已满");
            result.put("status", 0);
            return ResponseEntity.ok(result);
        }
        // 否则放进队列中
        rabbitTemplate.convertAndSend("ex.course", "course", courseSelectionDTO);
        result.put("msg", "选课排队中，排队时间预计2s到3s");
        result.put("status", 1);
        // 消费消息
        return ResponseEntity.ok(result);
    }

    @PostMapping("check-selection-status")
    public ResponseEntity<Object> checkSelectionStatus(@RequestBody CourseSelectionDTO courseSelectionDTO) {
        Integer i = courseService.checkSelectionStatus(courseSelectionDTO);
        Map<String, Object> result = new HashMap<>();
        result.put("status", i);
        if (i == 0) {
            result.put("msg", "选课未成功！请重新选课");
        } else {
            result.put("msg", "选课成功！");
        }
        return ResponseEntity.ok(result);
    }

    @RabbitListener(queues = "q.course")
    public void onMessage(CourseSelectionDTO courseSelectionDTO)  {
        int i = courseService.courseSelection(courseSelectionDTO);
        if (i == 1) {
            System.out.println("选课成功");
        } else {
            System.out.println("选课人数已满，课程号" + courseSelectionDTO.getCid());
        }
    }
}
